import React from 'react';
import { fetchEmployee } from '../Department/service';

const useEmployeeList = query => {
    const [data, setData] = React.useState([]);
    const [total, setTotal] = React.useState(0);
    const [loading, setLoading] = React.useState(true);

    const fetchData = async () => {
        setLoading(true);

        const { data: rawData = {}, code } = (await fetchEmployee(query)) || {};
        const { total: rawTotal = 0, rows = [] } = rawData;

        if (code === 10000) {
            setData(rows);
            setTotal(rawTotal);
        }

        setLoading(false);
    };

    React.useEffect(() => {
        if (query.departmentId) {
            fetchData();
        }
    }, [query]);

    return [loading, data, total];
};

export default useEmployeeList;
